﻿Imports System.Runtime.CompilerServices

Public Module StringExtensions
    <Extension()> _
         Public Function IsNothing(ByVal value As String) As Boolean

        Return value Is Nothing OrElse String.IsNullOrEmpty(value) OrElse String.IsNullOrEmpty(value.Trim())

    End Function

    ''' <summary>
    ''' Encryptes a string using the supplied key. Encoding is done using RSA encryption.
    ''' </summary>
    ''' <param name="stringToEncrypt">String that must be encrypted.</param>        
    ''' <returns>A string representing a byte array separated by a minus sign.</returns>
    ''' <exception cref="ArgumentException">Occurs when stringToEncrypt or key is null or empty.</exception>
    <Extension()> _
    Public Function Encrypt(ByVal stringToEncrypt As String) As String
        If String.IsNullOrEmpty(stringToEncrypt) Then
            Throw New ArgumentException("An empty string value cannot be encrypted.")
        End If
        Return Cryptography.Crypto.Encrypt(stringToEncrypt)
    End Function

    ''' <summary>
    ''' Encryptes a string using the supplied key. Encoding is done using RSA encryption.
    ''' </summary>
    ''' <param name="stringToEncrypt">String that must be encrypted.</param>        
    ''' <returns>A string representing a byte array separated by a minus sign.</returns>
    ''' <exception cref="ArgumentException">Occurs when stringToEncrypt or key is null or empty.</exception>
    <Extension()> _
    Public Function Encrypt(ByVal stringToEncrypt As String, ByVal CryptoKey As String) As String
        If String.IsNullOrEmpty(stringToEncrypt) Then
            Throw New ArgumentException("An empty string value cannot be encrypted.")
        End If
        Return Cryptography.Crypto.Encrypt(stringToEncrypt, CryptoKey)
    End Function

    <Extension()> _
    Public Function EncryptOneWay(ByVal stringToEncrypt As String) As String
        If String.IsNullOrEmpty(stringToEncrypt) Then
            Throw New ArgumentException("An empty string value cannot be encrypted.")
        End If
        Return Cryptography.Crypto.Encrypt(stringToEncrypt.GetMD5Hash())
    End Function

    <Extension()> _
    Private Function GetMD5Hash(ByVal input As String) As String
        Dim x As New System.Security.Cryptography.MD5CryptoServiceProvider()
        Dim bs As Byte() = System.Text.Encoding.UTF8.GetBytes(input)
        bs = x.ComputeHash(bs)
        Dim s As New System.Text.StringBuilder()
        For Each b As Byte In bs
            s.Append(b.ToString("x2").ToLower())
        Next
        Dim password As String = s.ToString()
        Return password
    End Function


    ''' <summary>
    ''' Decryptes a string using the supplied key. Decoding is done using RSA encryption.
    ''' </summary>
    ''' <param name="stringToDecrypt">String that must be decrypted.</param>         
    ''' <returns>The decrypted string or null if decryption failed.</returns>
    ''' <exception cref="ArgumentException">Occurs when stringToDecrypt or key is null or empty.</exception>
    <Extension()> _
    Public Function Decrypt(ByVal stringToDecrypt As String) As String
        Dim result As String = Nothing

        If String.IsNullOrEmpty(stringToDecrypt) Then
            Throw New ArgumentException("An empty string value cannot be encrypted.")
        End If
        Return Cryptography.Crypto.Decrypt(stringToDecrypt)
    End Function

    ''' <summary>
    ''' Decryptes a string using the supplied key. Decoding is done using RSA encryption.
    ''' </summary>
    ''' <param name="stringToDecrypt">String that must be decrypted.</param>         
    ''' <returns>The decrypted string or null if decryption failed.</returns>
    ''' <exception cref="ArgumentException">Occurs when stringToDecrypt or key is null or empty.</exception>
    <Extension()> _
    Public Function Decrypt(ByVal stringToDecrypt As String, ByVal CryptoKey As String) As String
        Try
            Dim result As String = Nothing

            If String.IsNullOrEmpty(stringToDecrypt) Then
                Throw New ArgumentException("An empty string value cannot be encrypted.")
            End If
            Return Cryptography.Crypto.Decrypt(stringToDecrypt, CryptoKey)
        Catch ex As Exception
            Throw ex
        End Try
    End Function


End Module
